//axios的基础路径
axios.defaults.baseURL="http://localhost:8080";
//全局axios属性
Vue.prototype.$http=axios;

//1.axios前置拦截器：每次通过axios发送请求之前，让所有的请求都携带uToken
axios.interceptors.request.use(config=>{
    //从localStorage中获取token
    let token =  localStorage.getItem("token");
    //如果有值
    if(token){
        //将token设置到请求头
        config.headers['token']=token;
    }
    return config;
},error => {
    Promise.reject(error);
})
//2.axios后置拦截器：后端处理完axios请求之后，处理响应的数据
axios.interceptors.response.use(res=>{
    let data = res.data;
    if (false === data.success && "noLogin" === data.msg) {
        localStorage.removeItem("token");
        localStorage.removeItem("logininfo");
        location.href="login.html"

    }
    return res;
},error => {
    Promise.reject(error);
})
//静态资源拦截器
let url=location.href;
if(url.indexOf("login.html")===-1&&url.indexOf("register.html")===-1
    &&url.indexOf("callback.html")===-1&&url.indexOf("binder.html")===-1) {
    //获取登录信息
    let logininfo = localStorage.getItem('logininfo');
    //没有登录信息到登录页
    if (logininfo == null) {
        //
        location.href = "login.html"
    }

}
//封装公用方法 - 将字符串的参数解析为json对象
function parseUrlParams2Obj(url) {
    let paramStr = url.substring(url.indexOf("?")+1);//xxx?code=xxx&state=1
    let paramArr = paramStr.split("&");
    let paramObj = {};
    for(let i = 0;i<paramArr.length;i++){
        let paramTemp = paramArr[i];
        let paramName = paramTemp.split("=")[0];
        let paramValue= paramTemp.split("=")[1];
        paramObj[paramName] = paramValue;
    }
    return paramObj;
}
